{extend name="public/base" /}
{block name="body"}
<div class="layui-fluid">
    <div class="layui-row">
        <div class="layui-col-sm12 layui-col-md12 layui-col-lg12">
           <div class="widget-head">
        <div class="widget-title">角色权限</div>
      </div>
      <div class="widget-body layui-col-lg12">
        <form class="layui-form" method="post">
              <div class="layui-form-item">
                  <div class="layui-form-block">            
                    <button type="button" class="layui-btn" onclick="checkAll('#LAY-auth-tree-index')">全选
                    </button>
                    <button type="button" class="layui-btn" onclick="uncheckAll('#LAY-auth-tree-index')">全不选
                    </button>
                    <button type="button" class="layui-btn" onclick="showAll('#LAY-auth-tree-index')">全部展开
                    </button>
                    <button type="button" class="layui-btn" onclick="closeAll('#LAY-auth-tree-index')">全部隐藏
                    </button>
                  </div>
              </div>
                <div class="layui-form-item">
                <label class="layui-form-label">选择权限</label>
                <div class="layui-input-block">
                    <div id="LAY-auth-tree-index"></div>
                </div>
            </div>                       
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <input type="hidden" id="id" name="id" value="{$info.id}">
                        <button class="layui-btn" type="submit" lay-submit lay-filter="LAY-auth-tree-submit">保存</button>
                        <button class="layui-btn layui-btn-primary" onclick="javascript:history.back(-1);return false;">返回</button>
                    </div>
                </div>
            </form>
      </div>
    </div>
  </div>
</div>
{/block}
{block name="js"}
<script type="text/javascript">
  layui.config({
      base: '/static/libs/extend/',
  }).extend({
      authtree: 'authtree',
  });

  layui.use(['authtree', 'form', 'layer'], function(){
    var authtree = layui.authtree;
    var form = layui.form;
    var layer = layui.layer;
    // 一般来说，权限数据是异步传递过来的
    $.ajax({
      url: '{:url("Menu/trees")}',
      dataType: 'json',
      success: function(data){
        var trees = data;
        // 如果后台返回的不是树结构，请使用 authtree.listConvert 转换
        authtree.render('#LAY-auth-tree-index', trees, {
          inputname: 'authids[]', 
          layfilter: 'lay-check-auth', 
          autowidth: true,
        });
      }
    });

    // 表单提交
    form.on('submit(LAY-auth-tree-submit)', function(obj){
      var authids = authtree.getChecked('#LAY-auth-tree-index');
      //console.log('Choosed authids is', authids);       
      $.ajax({
        type: "POST",
        url: '{:url("User/auth")}',           
        dataType: 'json',
        data: {id:$("#id").val(), menu_auth:authids},
        success: function(res){
          if (res.code === 1 && res.url != '') {
            setTimeout(function () {
              location.href = res.url;
            }, 1000);
          }
          layer.msg(res.msg);
        }
      });
      return false;
    });
  });

  // 全选
  function checkAll(dst){
    layui.use(['jquery', 'layer', 'authtree'], function(){
      var layer = layui.layer;
      var authtree = layui.authtree;

      authtree.checkAll(dst);
    });
  }

  // 全不选样例
  function uncheckAll(dst){
    layui.use(['jquery', 'layer', 'authtree'], function(){
      var layer = layui.layer;
      var authtree = layui.authtree;

      authtree.uncheckAll(dst);
    });
  }

  // 显示全部
  function showAll(dst){
    layui.use(['jquery', 'layer', 'authtree'], function(){
      var layer = layui.layer;
      var authtree = layui.authtree;

      authtree.showAll(dst);
    });
  }

  // 隐藏全部
  function closeAll(dst){
    layui.use(['jquery', 'layer', 'authtree'], function(){
      var layer = layui.layer;
      var authtree = layui.authtree;

      authtree.closeAll(dst);
    });
  } 
</script>
{/block}